﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sparrow.Reflection
{
    public class MethodProxy
    {
        public MethodProxy(object instance)
        {
            this.Instance = instance;
        }

        object Instance
        {
            get;
            set;
        }


        
        public Func<object[],object> this[string name]
        {
            get
            {
                var invoker = Instance.Metadata().Methods[name];

                if (invoker == null)
                {
                    return this.EmptyInvoker;
                }
                else
                {
                    return this.CreateInvokeDelegate(invoker.Invoke);
                }
              
            }
            set
            {
               //can NOT assign a new value to a method by reflection
            }
        }

        Func<object[], object> CreateInvokeDelegate(Func<object, object[], object> invoker)
        {
            return args => invoker(this.Instance, args);
        }

        object EmptyInvoker(params object[] args)
        {
            return null;
        }
    }
}
